package com.mitac.mcd.cs.mobiletracking.socketserver.report;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;



public class MobileTrackingReport {

	public static final Properties ConfigProperties = new Properties();
    public static final Properties jdbcProperties = new Properties();
    public static final String up_icon = "<font color='red' style='font-weight: bold;' size='2'>▲</font>";
    public static final String down_icon = "<font color='green' style='font-weight: bold;' size='2'>▼</font>";
	
	public MobileTrackingReport() throws IOException{
		BusinessUtil.sLogger.info("******************************************************");
        BusinessUtil.sLogger.info("初始化系统参数开始...");
        String configFilePath = "./config.properties";//System.getProperty("user.dir") + "\\config.properties";
        BusinessUtil.sLogger.info("configFilePath:"+configFilePath);
        InputStream ConfigInputStream = new BufferedInputStream(new FileInputStream(configFilePath));
        ConfigProperties.load(ConfigInputStream);
        String jdbcFilePath = "./jdbc.properties";//System.getProperty("user.dir") + "\\jdbc.properties";
        BusinessUtil.sLogger.info("jdbcFilePath:"+jdbcFilePath);
        InputStream jdbcInputStream = new BufferedInputStream(new FileInputStream(jdbcFilePath));
        jdbcProperties.load(jdbcInputStream);
        //init db connection pool
        try {
			DBConnection.initAutConnection();
			DBConnection.initGPSDATAConnection();
		} catch (Exception e) {
			BusinessUtil.sLogger.error("数据库连接池初始化发生异常:"+e);
			BusinessUtil.sLogger.error("MobileTrackingReport启动失败...");
			return;
		}
		//set daily task
        dailyTask();
        BusinessUtil.sLogger.info("日结任务定时器启动...");
        //set weekly task
        weeklyTask();
        BusinessUtil.sLogger.info("周结任务定时器启动...");
        //set monthly task
        monthlyTask();
        BusinessUtil.sLogger.info("月结任务定时器启动...");
	}
	
	/**
     * 设定每日日结任务
     */
    public void dailyTask(){
    	Timer timer = new Timer();
    	TimerTask dailyTimeTask = new TimerTask(){
    		public void run() {
    			DailyDataMaker.socketServerExceptionLogDailyReport();
    			DailyDataMaker.socketServerNotificationDailyReport();
    			DailyReportMaker.StatisticMailDaily();
    		}
    	};
    	Calendar date = Calendar.getInstance();
    	date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("DAILY_HOUR")));
    	date.set(Calendar.MINUTE, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("DAILY_MINUTE")));
    	date.set(Calendar.SECOND, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("DAILY_SECOND")));
    	long timestamp = 24*60*60*1000;
    	Calendar currentdate = Calendar.getInstance();
    	if (currentdate.after(date)){//如已过当日设定时间，则启动为明日此时
    		date.add(Calendar.DAY_OF_YEAR, 1);
    	}
    	/** *//**
    	* dailyTimeTask 指定执行的线程 date 指定第一次日期执行的时间 timestamp 指定每隔多久再次执行一次
    	*/
    	timer.schedule(dailyTimeTask, date.getTime(), timestamp);
    }
    
    /**
     * 设定每周周结任务
     */
    public void weeklyTask(){
    	Timer timer = new Timer();
    	TimerTask weeklyTimeTask = new TimerTask(){
    		public void run(){
    			WeeklyDataMaker.socketServerExceptionLogWeeklyReport();
    			WeeklyDataMaker.socketServerNotificationWeeklyReport();
    			WeeklyReportMaker.StatisticMailWeekly();
    		}
    	};
    	
    	Calendar date = Calendar.getInstance();
    	BusinessUtil.getLastDayOfWeek(date);
    	date.add(Calendar.DAY_OF_YEAR, 1);
    	date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("WEEKLY_HOUR")));
    	date.set(Calendar.MINUTE, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("WEEKLY_MINUTE")));
    	date.set(Calendar.SECOND, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("WEEKLY_SECOND")));
    	
    	long timestamp = 7*24*60*60*1000;
    	/** *//**
    	* weeklyTimeTask 指定执行的线程 date 指定第一次日期执行的时间 timestamp 指定每隔多久再次执行一次
    	*/
    	timer.schedule(weeklyTimeTask, date.getTime(), timestamp);
    }
    
    /**
     * 设定每月月结任务
     */
    public void monthlyTask(){
    	Timer timer = new Timer();
    	TimerTask monthlyTimeTask = new TimerTask(){
    		public void run(){
    			Calendar date = Calendar.getInstance();
    			if(date.get(Calendar.DAY_OF_MONTH)==1){//判断是否当月第一天
    				MonthlyDataMaker.socketServerExceptionLogMonthlyReport();
                	MonthlyDataMaker.socketServerNotificationMonthlyReport();
                	MonthlyReportMaker.StatisticMailMonthly();
    			}
    		}
    	};
    	long timestamp = 24*60*60*1000;
    	/** *//**
    	* weeklyTimeTask 指定执行的线程 date 指定第一次日期执行的时间 timestamp 指定每隔多久再次执行一次
    	*/
    	Calendar date = Calendar.getInstance();
    	date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("MONTHLY_HOUR")));
    	date.set(Calendar.MINUTE, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("MONTHLY_MINUTE")));
    	date.set(Calendar.SECOND, Integer.parseInt(MobileTrackingReport.ConfigProperties.getProperty("MONTHLY_SECOND")));
    	
    	Calendar currentdate = Calendar.getInstance();
    	if (currentdate.after(date)){//如已过当日设定时间，则启动为明日此时
    		date.add(Calendar.DAY_OF_YEAR, 1);
    	}
    	timer.schedule(monthlyTimeTask, date.getTime(), timestamp);
    }
    
    public static void main(String args[]) throws IOException{
    	new MobileTrackingReport();
    }
}
